package com.google.code.ptrends.entities.utils;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

import com.google.code.ptrends.common.entities.Category;
import com.google.code.ptrends.entities.ParametersStrings;

/**
 * 
 * @author Daniel
 * 
 */
public final class PrepareDBStrings {

	public static String generateParametersTableSQL() {
		String result = "";
		for (Field f : ParametersStrings.class.getDeclaredFields()) {
			if (f.getType() == String.class
					&& f.getModifiers() == (Modifier.FINAL + Modifier.PUBLIC + Modifier.STATIC)) {
				
					try {
						result = result
								.concat("insert into PARAMETERS (name) values ('"
										+ f.get(null) + "');");
					} catch (IllegalArgumentException e) {
						e.printStackTrace();
					} catch (IllegalAccessException e) {
						e.printStackTrace();
					}
				
			}
		}

		return result;
	}

	public static String generateSuppliersTableSQL() {
		return "insert into SUPPLIERS (name, website) values ('RET', 'www.ret.ru');"
				+ "insert into SUPPLIERS (name, website) values ('Key', 'www.key.ru');"
				+ "insert into SUPPLIERS (name, website) values ('DNS', 'www.dns-shop.ru');";
	}

	public static String generateClassesTableSQL() {
		return "insert into CLASSES (name) values ('General');";
	}

	public static String generateCategoriesTableSQL() {
		String result = "";

		for (Category cat : Category.values()) {
			result = result.concat("insert into CATEGORIES (name) values ('"
					+ cat.name() + "');");
		}

		return result;
	}

	public static String generateAllTablesSQL() {

		String prepareDBsql;

		final String sqlParameters = PrepareDBStrings.generateParametersTableSQL();
		final String sqlSuppliers = PrepareDBStrings.generateSuppliersTableSQL();
		final String sqlClasses = PrepareDBStrings.generateClassesTableSQL();
		final String sqlCategories = PrepareDBStrings.generateCategoriesTableSQL();

		prepareDBsql = sqlClasses + sqlSuppliers + sqlCategories + sqlParameters;

		return prepareDBsql;
	}

	private PrepareDBStrings() {

	}
}
